﻿using System;
using System.Windows.Forms;
using Rsdn.Janus.ObjectModel;

namespace Rsdn.Janus
{
    internal sealed class VirtualForumDummyForm : ForumDummyFormBase
    {
        private static VirtualForumDummyForm _instance;
        private int virtualForumId = -1;

        public VirtualForumDummyForm(IServiceProvider provider)
            : base(provider)
        {
            //ServiceManager.Publish<IDefaultCommandService>(
            //    new DefaultCommandService("Janus.Forum.GoToMessage"));

        }

        #region Instance Property

        /// <summary>
        /// Gets the instance. Make singleton
        /// </summary>
        /// <value>The instance.</value>
        public static VirtualForumDummyForm Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new VirtualForumDummyForm(ApplicationManager.Instance.ServiceProvider);
                }

                return _instance;
            }
        }
        #endregion

        internal override bool SelectMessage(IMsg msg, bool ignoreOldId)
        {
            if (!ignoreOldId && msg.ID == OldMsgId)
            {
                return false;
            }

            SetMessageView(msg);
            OldMsgId = msg.ID;

            IMsg curMsg = msg;

            IFeature activeFeature = Features.Instance.ActiveFeature;
            if(activeFeature is VirtualForum)
            {
                VirtualForumId = (activeFeature as VirtualForum).ID;
            }
            // Игрушечная асинхронность, которой на самом деле нет.
            // Однако позволяет не блокировать сразу цикл обработки сообщений,
            // что позволяет быстро перещелкивать сообщения в гриде с клавиатуры
            // без неприятного замедления времени отклика
            ForumNavigator forumNavigator = ApplicationManager.Instance.ForumNavigator;
            AsyncHelper.CreateOperation().PostOperationCompleted(
                () => forumNavigator.SelectMessage(VirtualForumId, curMsg.ID, ignoreOldId)); //force navigation
            return true;
        }

        //protected override bool ActionAfterActivateNode(TreeGrid.ITreeNode activatedNode)
        //{
        //    if (activatedNode == null)
        //    {
        //        return false;
        //    }

        //    IMsg msg = (IMsg)activatedNode;
        //    if (!SelectMessage(msg)) return false;


        //    StartMarkTimer();
        //    return true;
        //}


        protected override void UpdateColumnTitle(ColumnHeader columnHeader, ImageList imageList)
        {
            switch (columnHeader.Index)
            {
                case ColumnSubjectRate:
                    columnHeader.Text = SR.TGColumnForumName;
                    break;
                default:
                    base.UpdateColumnTitle(columnHeader, imageList);
                    break;
            }
        }

        protected override SortType TranslateColumnToSortOrder(int columnId, SortType sort)
        {
            switch (columnId)
            {
                case ColumnRate:
                //case ColumnSubjectRate:
                    sort = sort == SortType.BySubjectRateAsc
                               ? SortType.BySubjectRateDesc
                               : SortType.BySubjectRateAsc;
                    break;

                case ColumnDate:
                    sort = sort == SortType.ByDateAsc
                               ? SortType.ByDateDesc
                               : SortType.ByDateAsc;
                    break;

                default:
                    return base.TranslateColumnToSortOrder(columnId, sort);
            }
            return sort;
        }

        /// <summary>
        /// Gets or sets the virtual forum id.
        /// </summary>
        /// <value>The virtual forum id.</value>
        public int VirtualForumId
        {
            get { return virtualForumId; }
            set { virtualForumId = value; }
        }

    }
}